package com.wisdytech.linkdcs.pda.service.impl;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.github.pagehelper.PageInfo;
import com.wisdytech.common.bean.Filter;
import com.wisdytech.common.constants.CommonConstants;
import com.wisdytech.linkdcs.common.BaseUtils;
import com.wisdytech.linkdcs.common.handler.BaseService;
import com.wisdytech.linkdcs.equipLifeCycle.check.dao.ICheckDao;
import com.wisdytech.linkdcs.equipLifeCycle.check.dao.ICheckModelItemDao;
import com.wisdytech.linkdcs.equipLifeCycle.check.dto.CheckSearchDto;
import com.wisdytech.linkdcs.equipLifeCycle.check.model.Check;
import com.wisdytech.linkdcs.equipLifeCycle.check.model.CheckDetailRecord;
import com.wisdytech.linkdcs.equipLifeCycle.check.model.CheckModelItem;
import com.wisdytech.linkdcs.equipLifeCycle.check.model.CheckRecord;
import com.wisdytech.linkdcs.equipLifeCycle.check.vo.CheckVo;
import com.wisdytech.linkdcs.pda.service.ICheckService;

@Service
@Transactional
public class CheckServiceImpl extends BaseService<ICheckDao, Check> implements ICheckService {

	@Override
	public PageInfo<CheckVo> listEquipChecks(Filter filter, CheckSearchDto dto) {
		List<CheckVo>  result=
				this.beforeList(ICheckDao.class, filter).selectCheckRecord(dto);
		
		return new PageInfo<CheckVo>(result);
	}

	@Override
	public PageInfo<CheckModelItem> listCheckModelItems(Filter filter, String checkId) {
		List<CheckModelItem>  reuslt =this.beforeList(ICheckModelItemDao.class, filter)
				.selectDetailByCheckId(checkId);
			
		return new PageInfo<CheckModelItem>(reuslt);
	}

	@Override
	public boolean addCheckRecord(List<CheckDetailRecord> records, String equipCode, String checkTimeSpan,
			String createUser) {
		Date date =new Date();
		String recordId=BaseUtils.getUUID();
		//整体检验是否成功
		boolean checkResult =true;
		
		//保存设备点检记录明细
		//补充记录信息
		for(CheckDetailRecord cr : records) {
			//有校验失败
			if(CommonConstants.CHECK_ERROR.equals(cr.getCheckResult())){
				checkResult=false;
			}
			
			cr.setPkId(BaseUtils.getUUID());
			cr.setCheckRecordId(recordId);
			cr.setDelFlag(CommonConstants.DEL_FLAG);
			cr.setCreateUser(createUser);
			cr.setCreateDate(date);
			cr.setUpdateDate(date);
			cr.insert();
		}
		
		
		//保存设备点检记录
		CheckRecord record =new CheckRecord();
		record.setPkId(recordId);
		record.setCheckTimeSpan(checkTimeSpan);
		record.setCheckDate(date);
		record.setCheckUser(createUser);
		record.setEquipCode(equipCode);
		record.setDelFlag(CommonConstants.DEL_FLAG);
		record.setCreateUser(createUser);
		record.setCreateDate(date);
		record.setUpdateDate(date);
		
		Calendar calendar =Calendar.getInstance();
		calendar.setTime(date);
		record.setYear(String.valueOf(calendar.get(Calendar.YEAR)));
		record.setMonth(String.valueOf(calendar.get(Calendar.MONTH)+1));
		record.setDay(String.valueOf(calendar.get(Calendar.DAY_OF_MONTH)));
		
		//根据点检明细结果判断是否合格
		record.setCheckResult(
				checkResult?CommonConstants.CHECK_OK:CommonConstants.CHECK_ERROR);
		
		//修改点检列表最新点检时间
		Check check =new Check();
		check.setLastDate(date);
		check.setUpdateUser(createUser);
		check.setUpdateDate(date);
		EntityWrapper<Check> ew=new EntityWrapper<>();
		check.update(ew.eq("EQUIP_CODE", equipCode));
		
		return record.insert();
	}

}
